package com.bupt.kgplatform.common;


import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author ljr
 * @date 2020/12/17 10:57
 * @Version 1.0  上传文件到文件服务器上
 */
@Component

public class FTPUtil {

    private static String host;
    @Value(value = "${ftp-server.host}")
    public  void setHost(String hostName){
        host = hostName;
    }

    private static Integer port;
    @Value(value = "${ftp-server.port}")
    public  void setPort(Integer portName) {
        port = portName;
    }

    private static String user;
    @Value(value = "${ftp-server.user}")
    public  void setUser(String userName) {
        user = userName;
    }

    private static String password;
    @Value(value = "${ftp-server.password}")
    public  void setPassword(String passwordName) {
        password = passwordName;
    }

    private static String basePath;
    @Value(value = "${ftp-server.basePath}")
    public  void setBasePath(String basePath) {
        FTPUtil.basePath = basePath;
    }

    private static ChannelSftp sftp;

    private static Session session;

    public static void sftpLogin(){
        JSch jsch = new JSch();
        try{
            //采用指定的端口连接服务器
            session = jsch.getSession(user,host,port);
            if (session == null) {
                throw new Exception("session is null");
            }
            if(password != null){
                //设置登陆主机的密码
                session.setPassword(password);
            }
            //优先使用 password 验证   注：session.connect()性能低，使用password验证可跳过gssapi认证，提升连接服务器速度
            session.setConfig("PreferredAuthentications","password");
            //设置第一次登陆的时候提示，可选值：(ask | yes | no)
            session.setConfig("StrictHostKeyChecking","no");
            session.connect(30000);
            //创建sftp通信通道
            Channel channel = session.openChannel("sftp");
            channel.connect(1000);
            sftp = (ChannelSftp) channel;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void sftpLogout(){
        if(sftp != null){
            if(sftp.isConnected()){
                sftp.disconnect();
            }
        }
        if(session != null){
            if(session.isConnected()){
                session.disconnect();
            }
        }
    }

    public static void Sftpupload(byte[] bytes, String fileName, String relativepath) throws Exception {
        OutputStream outstream = null;
        try {
            //进入服务器指定的文件夹
            sftp.cd(basePath);
            //如果文件夹不存在，则创建文件夹,通过报错实现
            try{
                sftp.ls(relativepath);
            }
            catch (Exception e){
                sftp.mkdir(relativepath);
            }
            //进入文件夹
            sftp.cd(relativepath);

//            列出服务器指定的文件列表
//                    Vector v = sftp.ls("*");
//                    for(int i=0;i<v.size();i++){
//                        System.out.println(v.get(i));
//                    }
            //以下代码实现从本地上传一个文件到服务器
            outstream = sftp.put(fileName);
            outstream.write(bytes);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关流操作
            if (outstream != null) {
                outstream.flush();
                outstream.close();
            }
        }
    }

    public static InputStream Sftpdownload(String fileName, String relativepath) throws Exception {
        try {
            //进入服务器指定的文件夹
            sftp.cd(basePath);
            sftp.cd(relativepath);
            //如果文件夹不存在，则创建文件夹,通过报错实现
            return sftp.get(fileName);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    public static void Sftpdelete(String fileName, String relativepath){
        try{
            sftp.cd(basePath);
            sftp.cd(relativepath);
            sftp.rm(fileName);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}